﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TemplateGeneratorApp.Core
{
    public class SlnBuilder
    {
        public SlnBuilder()
        {
            _header = @"Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1";

            _global = @$"Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|Any CPU = Debug|Any CPU
		Release|Any CPU = Release|Any CPU
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
$temp
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
	GlobalSection(ExtensibilityGlobals) = postSolution
		SolutionGuid = {{{Guid.NewGuid()}}}
	EndGlobalSection
EndGlobal
";
        }


        public void AddProject(string name,string path)
        {
            _projects.Add(new ProjectSlnModel(name, path));
        }

        public void InsertProject(string name, string path, int idx)
        {
            _projects.Insert(idx, new ProjectSlnModel(name, path));
        }

        public string BuildProjectString()
        {
            var sb = new StringBuilder();
            sb.Append(_header);
            var tempStr = "";
            sb.Append("\n");
            for (var i = 0; i < _projects.Count; i++)
            {
                var project = _projects[i];
                sb.Append(@$"Project(""{{{project.ProjectId}}}"") = ""{project.Name}"", ""{project.Path}"", ""{{{project.BuildId}}}""
EndProject" + "\n");

                tempStr += @$"		{{{project.BuildId}}}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
		{{{project.BuildId}}}.Debug|Any CPU.Build.0 = Debug|Any CPU
		{{{project.BuildId}}}.Release|Any CPU.ActiveCfg = Release|Any CPU";
                if (i < _projects.Count - 1)
                {
                    tempStr += "\n";
                }
            }

            var globalStr = _global.Replace("$temp", tempStr);

            sb.Append(globalStr);

            return sb.ToString();
        }

        private List<ProjectSlnModel> _projects = new List<ProjectSlnModel>();

        private string _header;

        private string _global;

    }

    public class ProjectSlnModel
    {
        public ProjectSlnModel(string name,string path)
        {
            Name = name;
            Path = path;
            ProjectId = Guid.NewGuid();
            BuildId = Guid.NewGuid();
        }

        public string Name { get; private set; }
        public string Path { get; private set; }

        public Guid ProjectId { get; private set; }

        public Guid BuildId { get; private set; }

    }


}

